package com.classtest.classtestdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.classtest.classtestdemo.bean.DbGoods;
import com.classtest.classtestdemo.bean.Image;
import com.classtest.classtestdemo.bean.Images;
import com.classtest.classtestdemo.bean.Limit;
import com.classtest.classtestdemo.service.DbGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/goodsList")
@RestController
public class DbGoodsController {
    @Autowired
    DbGoodsService goodsService;

    final HttpHeaders headers = new HttpHeaders();

    @RequestMapping(value = "/fuzzySearchAll")
    public String fuzzySearchAll(@RequestBody String json){
        headers.setContentType(MediaType.IMAGE_PNG);


        Map<String,Object> result = new HashMap<String,Object>();
        List<Object> list = new ArrayList<>();
        String resFlag = null;
        List<DbGoods> dbGoods = new ArrayList<>();
        List<Object> list1 = new ArrayList<>();

        try{
            if(goodsService.fuzzySearchAll(json).isEmpty()){
                resFlag = "ok";
                result.put("resMsg","没有找到符合搜索要求的商品");
            }else {
                resFlag = "ok";
                dbGoods = goodsService.fuzzySearchAll(json);
                int count =goodsService.fuzzyCount(json);
                result.put("count",count);

                for (DbGoods goods:dbGoods
                ) {
                    Map<String,Object> res = new HashMap<>();
                    String imagesrc = goods.getGsImageW();
                    String path = goods.getGsImageR();
                    res.put("key",goods.getGsId());
                    res.put("id",goods.getGsId());
                    res.put("size",goods.getGsSize());
                    res.put("name",goods.getGsName());
                    res.put("price",goods.getGsPrice());
                    res.put("desc",goods.getGsDesc());

                    File fl = new File(path+"\\"+goods.getGsMainImage());
                    FileInputStream fileInputStream = new FileInputStream(fl);

                    byte[] bytes = getBytesByStream(fileInputStream);

                    res.put("src","http://localhost:8181/image/"+goods.getGsMainImage());
                    Images images = JSONObject.parseObject(imagesrc,Images.class);
                    List<Image> image = images.getImages();
                    for (Image im:image
                         ) {
                        Map<String,Object> map = new HashMap<>();
                        File file = new File(path+"\\"+im.getSrc());
                        FileInputStream fileInputStream1 = new FileInputStream(file);

                        byte[] bytes1 = getBytesByStream(fileInputStream1);

                        map.put("src","http://localhost:8181/image/"+im.getSrc());
                        list1.add(map);
                    }
                    res.put("images",list1);
                    list.add(res);
                }
            }

        }catch (Exception e){
            result.put("error",e.toString());
        }
        result.put("goods",list);
        result.put("resFlag",resFlag);
        return JSON.toJSONString(result);
    }
    @RequestMapping(value = "/selectAllLimit")
    public String selectAll(@RequestBody String json){
        headers.setContentType(MediaType.IMAGE_PNG);
        Map<String,Object> result = new HashMap<>();
        List<Object> list = new ArrayList<>();
        List<Object> list1 = new ArrayList<>();

        try{
            Limit limit = JSONObject.parseObject(json, Limit.class);

            if (limit.getSearchName() == null){
                int count = goodsService.selectCount();
                result.put("count",count);
                List<DbGoods> dbGoods = goodsService.selectAll(limit.getDown(),limit.getUp());

                for (DbGoods goods:dbGoods
                ) {
                    Map<String,Object> res = new HashMap<>();
                    String imagesrc = goods.getGsImageW();
                    String path = goods.getGsImageR();
                    res.put("key",goods.getGsId());
                    res.put("id",goods.getGsId());
                    res.put("name",goods.getGsName());
                    res.put("size",goods.getGsSize());
                    res.put("buyerId",goods.getUsId());
                    res.put("count",goods.getGsCount());
                    res.put("color",goods.getGsColor());
                    res.put("price",goods.getGsPrice());
                    res.put("desc",goods.getGsDesc());
//                File fl = new File(path+"\\"+goods.getGsMainImage());
//                FileInputStream fileInputStream = new FileInputStream(fl);
//
//                byte[] bytes = getBytesByStream(fileInputStream);

                    res.put("src","http://localhost:8181/image/"+goods.getGsMainImage());
                    Images images = JSONObject.parseObject(imagesrc,Images.class);
                    List<Image> image = images.getImages();
                    for (Image im:image
                    ) {
                        Map<String,Object> map = new HashMap<>();
                        File file = new File(path+"\\"+im.getSrc());
                        FileInputStream fileInputStream1 = new FileInputStream(file);

                        byte[] bytes1 = getBytesByStream(fileInputStream1);

                        map.put("src","http://localhost:8181/image/"+im.getSrc());
                        list1.add(map);
                    }
                    res.put("images",list1);
                    list.add(res);
            }
            }else {
                return fuzzySearchAll(json);
            }
        }catch (Exception e){
            result.put("error",e.toString());
        }
        result.put("goods",list);
        return JSON.toJSONString(result);
    }

    public static byte[]  getBytesByStream(FileInputStream inputStream){
        byte[] bytes = new byte[1024];

        int b;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            while((b = inputStream.read(bytes)) != -1){

                byteArrayOutputStream.write(bytes,0,b);
            }
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
